package com.yfl.config;

import com.sun.el.parser.Token;
import jdk.internal.dynalink.support.NameCodec;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;

import java.security.KeyPair;

/**
 * 1.token的存储
 * 2.jwt的转换器
 * 3.第三方应用
 * 4.暴露服务
 */
@Configuration
public class AuthorizationConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    /**
     * token的存储方式
     */
    @Bean
    public TokenStore tokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        // 使用非对称加密
        ClassPathResource resource = new ClassPathResource("yfl-jwt.jks");
        // 创建钥匙工厂
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(resource, "123456".toCharArray());
        KeyPair keyPair = keyStoreKeyFactory.getKeyPair("yfl-jwt");
        // 设置钥匙
        jwtAccessTokenConverter.setKeyPair(keyPair);
        return jwtAccessTokenConverter;

    }
    /**
     * 配置第三方应用
     * password 密码授权的方式 我们认为前端应用 就是第三方应用
     * client_credentials 给我们自己的服务内部使用
     *
     * @param clients
     * @throws Exception
     */
    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory()
                .withClient("web")
                .secret(passwordEncoder().encode("web-wecret"))
                .scopes("all")
                .authorizedGrantTypes("password") // 密码授权
                .accessTokenValiditySeconds(21600) // 给 6个小时
                .redirectUris("https://www.baidu.com")
                .and()
                .withClient("client")
                .secret(passwordEncoder().encode("client-secret"))
                .scopes("write")
                .authorizedGrantTypes("client_credentials") // 客户端授权
                .accessTokenValiditySeconds(Integer.MAX_VALUE)  // 66 年
                .redirectUris("https://www.baidu.com");
    }
    /**
     * 暴露服务
     *
     * @param endpoints
     * @throws Exception
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
        endpoints.tokenStore(tokenStore())
                .accessTokenConverter(jwtAccessTokenConverter())
                .authenticationManager(authenticationManager);
    }


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
